import 'package:flutter/material.dart';
import '../../services/camera/camera_service.dart';
import '../../services/ml/tflite_service.dart';
import '../../widgets/camera/camera_preview.dart';

/// 主页面
/// 应用的主界面，负责显示相机预览和姿态检测结果
/// 创建时间：2024-01-23

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
  final CameraService _cameraService = CameraService();
  final TFLiteService _tfliteService = TFLiteService();

  @override
  void initState() {
    super.initState();
    _initializeServices();
  }

  Future<void> _initializeServices() async {
    await _cameraService.initializeCamera();
    await _tfliteService.loadModel();
    setState(() {});
  }

  @override
  void dispose() {
    _cameraService.dispose();
    _tfliteService.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Virtual Fitness Coach')),
      body: Column(
        children: <Widget>[
          Expanded(
            child: _cameraService.controller != null &&
                    _cameraService.controller!.value.isInitialized
                ? CameraPreviewWidget(
                    cameraController: _cameraService.controller)
                : const Center(child: CircularProgressIndicator()),
          ),
        ],
      ),
    );
  }
}
